package helloworld.admin.form.course;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.Globals;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.ImageButtonBean;
import org.apache.struts.util.MessageResources;
import helloworld.example.util.BaseActionForm;

/** 
 * @author TUYEN-DM
 *         <p>
 *         2009/05/09
 *         </p>
 *         <p>
 *         CourseForm is the object representation of the html form for entering
 *         course's name in helloworld.
 *         </p>
 */
public class CourseForm extends BaseActionForm {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/** Course Name */
	private String courseName;
	private String content;
	private String openDate;
	private String closeDate;
	private int classes;
	private String fee;
	private String qualification;
	private String recruitTime;

	private boolean isExisting;

	private int courseID;

	private ImageButtonBean save;
	private ImageButtonBean cancel;

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getOpenDate() {
		return openDate;
	}

	public void setOpenDate(String openDate) {
		this.openDate = openDate;
	}

	public String getCloseDate() {
		return closeDate;
	}

	public void setCloseDate(String closeDate) {
		this.closeDate = closeDate;
	}

	public int getClasses() {
		return classes;
	}

	public void setClasses(int classes) {
		this.classes = classes;
	}

	public String getFee() {
		return fee;
	}

	public void setFee(String fee) {
		this.fee = fee;
	}

	public String getQualification() {
		return qualification;
	}

	public void setQualification(String qualification) {
		this.qualification = qualification;
	}

	public String getRecruitTime() {
		return recruitTime;
	}

	public void setRecruitTime(String recruitTime) {
		this.recruitTime = recruitTime;
	}

	public void setExisting(boolean isExisting) {
		this.isExisting = isExisting;
	}

	public CourseForm() {
		// Dont forget to Initialize here
		// Also dont forget to init in reset();
		init();
	}

	// Without this method to initialization, non-primitive objects such as
	// Address, ImageButtonBean will throw NullPointerException
	// or might not work as expected
	// since a getAddress(), getSave() etc. returns a null object
	protected void init() {
		courseName = "";
		content = "";
		openDate = "";
		closeDate = "";
		classes = 0;
		fee = "";
		qualification = "";
		recruitTime = "";
		save = new ImageButtonBean();
		cancel = new ImageButtonBean();
		isExisting = false;
		courseID = 0;
	}

	public int getCourseID() {
		return courseID;
	}

	public void setCourseID(int i) {
		courseID = i;
	}

	public String getCourseName() {
		return courseName;
	}

	public void setCourseName(String s) {
		this.courseName = s;
	}

	public ImageButtonBean getCancel() {
		return cancel;
	}

	public ImageButtonBean getSave() {
		return save;
	}

	public boolean getIsExisting() {
		return isExisting;
	}

	public void setIsExisting(boolean b) {
		isExisting = b;
	}

	/**
	 * This method is called by the RequestProcessor immediately after
	 * populating the CourseForm only if the validate=true flag is set in the
	 * helloworld-config.xml Do validation here and return ActionErrors if any,
	 * to the RequestProcessor
	 */
	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
		ActionErrors errors = new ActionErrors();

		// Need to do this explicitly now. Since the Button is not grey and its
		// name is not
		// org.apache.struts...Cancel anymore, and the validate=true still
		// remains in ActionMapping, the call to bypass validation on clicking
		// cancel image
		// has to be performed manually
		if (getCancel().isSelected()) {
			return errors;
		}

		MessageResources msgRes = (MessageResources) request
				.getAttribute(Globals.MESSAGES_KEY);

		// CourseName cannot be empty
		if (courseName == null || courseName.trim().equals("")) {
			String courseName = msgRes.getMessage("course.coursename");
			String[] rplcmntValueArr = { courseName };
			ActionError err = new ActionError("error.required", rplcmntValueArr);
			errors.add("courseName", err);
		}
		return errors;
	}

}